import { Controller } from "../../mvc/core/Controller";
import { IView } from "../../mvc/interfaces/IView";
import { CrazyFishProgressView } from "../view/fishActive/CrazyFishProgressView";
import { GameLayout } from "../GameLayout";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { SActiveToClient } from "../../../Message/Public/SActiveToClient";
import { Cache } from "../cache/Cache";
import { EActiveType } from "../../../Message/Public/EActiveType";
import { SCrazyFishNum } from "../../../Message/Public/SCrazyFishNum";
import { DataEvent } from "../events/DataEvent";
//class FishActiveController
    
    export  class FishActiveController extends Controller
    {
       

        protected /* override */ initView(): IView
        {
            if (!this._crazyFishView) 
            {
                this._crazyFishView = new CrazyFishProgressView();
                GameLayout.instance.resize();
            }
            return this._crazyFishView;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.CrazyFishActiveCommand, this.onCrazyFishActiveCommand);
            NetDispatcher.addCmdListener(ServerCommand.FishCrazyNum, this.onFishCrazyNumHandler);
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdate);
            return;
        }

        /* internal  */onCrazyFishActiveCommand(arg1: SActiveToClient): void
        {
            this._isInFishActive = Cache.instance.active.isActiveInEffect(EActiveType._EActiveTypeCrazyFish);
            this.updateView();
            return;
        }

        /* internal  */onFishCrazyNumHandler(arg1: SCrazyFishNum): void
        {
            this._crazyFishNum = arg1;
            this.updateView();
            return;
        }

        /* internal  */onSceneUpdate(arg1: DataEvent): void
        {
            if (!this._isInSelfGuildMap && cache.guild.isInSelfManorMap()) 
            {
                this._isInSelfGuildMap = true;
                this.updateView();
            }
            else if (this._isInSelfGuildMap && !cache.guild.isInSelfManorMap()) 
            {
                this._isInSelfGuildMap = false;
                this.updateView();
            }
            return;
        }

        /* internal  */updateView(): void
        {
            if (this._isInSelfGuildMap && this._isInFishActive && this._crazyFishNum) 
            {
                view.show();
                (view as CrazyFishProgressView).updateCrazyNum(this._crazyFishNum.fishNum);
            }
            else if (this.initView()) 
            {
                view.hide();
            }
            return;
        }

        public isInitView(): Boolean
        {
            return !(this._crazyFishView == null);
        }

        private /* var */_isInFishActive: Boolean=false;

        private /* var */_crazyFishNum: SCrazyFishNum;

        private /* var */_isInSelfGuildMap: Boolean=false;

        private /* var */_crazyFishView: CrazyFishProgressView;
    }
